#include "hal_data.h"
#include <stdio.h>
#include "smg.h"
#include "timer_smg.h"
#include "dht11.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

//数码管变量
uint8_t num1=0,num2=0,num3=0,num4=0;//4个数码管显示的数值
uint8_t num_flag=0;//4个数码管和冒号轮流显示，一轮刷新五次

//温湿度变量
uint8_t humdity_integer;//湿度整数
uint8_t humdity_decimal;//湿度⼩数
uint8_t temp_integer ;//温度整数
uint8_t temp_decimal ;//温度⼩数
uint8_t dht11_check ;//校验值

//RTC变量
/* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
rtc_time_t set_time =
{
 .tm_sec = 50, /* 秒，范围从 0 到 59 */
 .tm_min = 59, /* 分，范围从 0 到 59 */
 .tm_hour = 23, /* ⼩时，范围从 0 到 23*/
 .tm_mday = 29, /* ⼀⽉中的第⼏天，范围从 0 到 30*/
 .tm_mon = 11, /* ⽉份，范围从 0 到 11*/
 .tm_year = 123, /* ⾃ 1900 起的年数，2023为123*/
 .tm_wday = 6, /* ⼀周中的第⼏天，范围从 0 到 6*/
// .tm_yday=0, /* ⼀年中的第⼏天，范围从 0 到 365*/
// .tm_isdst=0; /* 夏令时*/
};

//RTC闹钟变量
rtc_alarm_time_t set_alarm_time=
{
 .time.tm_sec = 58, /* 秒，范围从 0 到 59 */
 .time.tm_min = 59, /* 分，范围从 0 到 59 */
 .time.tm_hour = 23, /* ⼩时，范围从 0 到 23*/
 .time.tm_mday = 29, /* ⼀⽉中的第⼏天，范围从 1 到 31*/
 .time.tm_mon = 11, /* ⽉份，范围从 0 到 11*/
 .time.tm_year = 123, /* ⾃ 1900 起的年数，2023为123*/
 .time.tm_wday = 6, /* ⼀周中的第⼏天，范围从 0 到 6*/
 .sec_match = 1,//每次秒到达设置的进⾏报警
 .min_match = 0,
 .hour_match = 0,
 .mday_match = 0,
 .mon_match = 0,
 .year_match = 0,
 .dayofweek_match = 0,
 };

bsp_io_level_t sw1;//按键SW1状态
bsp_io_level_t sw2;//按键SW2状态
bsp_io_level_t sw3;//按键SW3状态
bsp_io_level_t sw4;//按键SW4状态
bsp_io_level_t qe_sw;//触摸电容状态
int sw1_num1=0;//按键SW1计数值，去抖和⻓按短按判断
int sw2_num1=0;//按键SW2计数值，去抖和⻓按短按判断
int sw3_num1=0;//按键SW3计数值，去抖和⻓按短按判断
int sw4_num1=0;//按键SW4计数值，去抖和⻓按短按判断
int qe_sw_num1=0;//触摸按键计数值，去抖和⻓按短按判断
void qe_touch_sw(void);
//数码管显示状态，0正常显示，1修改⼩时，2修改分钟，3保存修改数据，4温度，5湿度
int smg_mode=0;
int sec=0,min=0,hour=0;//保存时间数据
uint16_t time_mode_num=0;//定时器刷新时间，实现闪烁效果



//RTC回调函数
volatile bool rtc_flag = 0;//RTC延时1s标志位
volatile bool rtc_alarm_flag = 0;//RTC闹钟
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
 /* TODO: add your own code here */
 if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
 rtc_flag=1;
 else if(p_args->event == RTC_EVENT_ALARM_IRQ)
 rtc_alarm_flag=1;
}

/* Callback function */
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
 if(p_args->event == UART_EVENT_TX_COMPLETE)
 {
 uart_send_complete_flag = true;
 }
}

//串⼝重定向
#ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
 err = R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
 if(FSP_SUCCESS != err) __BKPT();
 while(uart_send_complete_flag == false){}
 uart_send_complete_flag = false;
 return ch;
}
int _write(int fd,char *pBuffer,int size)
{
 for(int i=0;i<size;i++)
 {
 __io_putchar(*pBuffer++);
 }
 return size;
}



/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{

    buzzer();
    //DHT11初始化
    R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_03_PIN_01,BSP_IO_LEVEL_HIGH);
    R_BSP_SoftwareDelay(1000U, BSP_DELAY_UNITS_MILLISECONDS);

    //打开串口
    /* TODO: add your own code here */
    fsp_err_t err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
    assert(FSP_SUCCESS == err);

    //测试数码管
    // smg_3(6);
    // R_BSP_SoftwareDelay(1000U, BSP_DELAY_UNITS_MILLISECONDS);
   // ceshi_smg();

//R_IOPORT_PinWrite(&g_ioport_ctrl,BSP_IO_PORT_01_PIN_03,BSP_IO_LEVEL_HIGH);
/***********************开启定时器**********************************/
    err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);           //2ms进一次定时器中断回调,timer_smg.c
    assert(FSP_SUCCESS == err);
    (void) R_GPT_Start(&g_timer0_ctrl);


    /**********************RTC开启***************************************/
     /* Initialize the RTC module*/
     err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
     /* Handle any errors. This function should be defined by the user.
    */
     assert(FSP_SUCCESS == err);
     /* Set the RTC clock source. Can be skipped if "Set Source Clock i
    n Open" property is enabled. */
     R_RTC_ClockSourceSet(&g_rtc0_ctrl);
     /* R_RTC_CalendarTimeSet must be called at least once to start the
    RTC */
     R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
     /* Set the periodic interrupt rate to 1 second */
     R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_2_SECOND);
     R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
     uint8_t rtc_second= 0; //秒
     uint8_t rtc_minute =0; //分
     uint8_t rtc_hour =0; //时
     uint8_t rtc_day =0; //⽇
     uint8_t rtc_month =0; //⽉
     uint16_t rtc_year =0; //年
     uint8_t rtc_week =0; //周
     rtc_time_t get_time;

     //获取RTC时间
     sec=set_time.tm_sec;
     min=set_time.tm_min;
     hour=set_time.tm_hour;


    while(1)
    {
        if(rtc_flag)
              {
              R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
              rtc_flag=0;
              rtc_second=get_time.tm_sec;//秒
              rtc_minute=get_time.tm_min;//分
              rtc_hour=get_time.tm_hour;//时
              rtc_day=get_time.tm_mday;//⽇
              rtc_month=get_time.tm_mon;//⽉
              rtc_year=get_time.tm_year; //年
              rtc_week=get_time.tm_wday;//周
              printf(" %d y %d m %d d %d h %d m %d s %d w\r\n",rtc_year+1900,rtc_month,rtc_day,rtc_hour,rtc_minute,rtc_second,rtc_week);

              //时间显示
              num1=rtc_hour/10;
              num2=rtc_hour%10;
              num3=rtc_minute/10;
              num4=rtc_minute%10;

              //每过一分钟更新时间
              if(rtc_second==0&&smg_mode==0)//这个时候刷新变量
               {
               sec=rtc_second;//时间数据 秒
               min=rtc_minute;//时间数据 分钟
               hour=rtc_hour;//时间数据 ⼩时
               }


              }
              if(rtc_alarm_flag)
              {
              rtc_alarm_flag=0;
              printf("/************************Alarm Clock********************************/\r\n");
              }

              //处理按键
             // set_smg_button();

              R_BSP_SoftwareDelay(10U, BSP_DELAY_UNITS_MILLISECONDS);
     //printf("hello world!\r\n");
     //R_BSP_SoftwareDelay(4000U, BSP_DELAY_UNITS_MILLISECONDS);
     //DHT11_Read();
     //printf("hum=%d temp=%d\r\n",humdity_integer,temp_integer);
    }

#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
FSP_CPP_FOOTER

#endif
